package com.music.service.members.dao.impl;

import static com.music.service.members.constants.MemberSQLQueries.GET_MEMBER_BY_EMAILID;
import static com.music.service.members.constants.MemberSQLQueries.GET_MEMBER_BY_MEMBER_ID;
import static com.music.service.members.constants.MemberSQLQueries.INSERT_MEMBER;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.music.service.members.beans.Member;
import com.music.service.members.constants.Gender;
import com.music.service.members.dao.MemberDAO;
import com.music.service.members.mapper.MemberMapper;

public class MemberDAOImpl implements MemberDAO {
	private JdbcTemplate mMusicsTemplate;

	
	/**
	 * @return the musicsTemplate
	 */
	public JdbcTemplate getMusicsTemplate() {
		return mMusicsTemplate;
	}
	/**
	 * @param pMusicsTemplate the musicsTemplate to set
	 */
	@Required
	public void setMusicsTemplate(JdbcTemplate pMusicsTemplate) {
		mMusicsTemplate = pMusicsTemplate;
	}
	
	@Override
	public Member createMember(String pFirstName, String pLastName,
			String pPassword, Date pBirthDate, Gender pGender, final String pEmail) throws Exception {
		int nRowsUpdated = getMusicsTemplate().update(INSERT_MEMBER,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setString(1, pEmail);
					}
				});
		if(nRowsUpdated == 1){
			return getMember(pEmail);
		}else{
			throw new Exception("Unable to create member");
		}
	
	}

	@Override
	public boolean deleteMember(Long pMemberId) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Member getMember(final Long pMemberId) {
		List<Member> member = null;
		member = getMusicsTemplate().query(GET_MEMBER_BY_MEMBER_ID,
					new PreparedStatementSetter() {
						public void setValues(PreparedStatement preparedStatement)
								throws SQLException {
							preparedStatement.setLong(1, pMemberId);
						}
					}, new MemberMapper());
		if(member != null && !member.isEmpty()){
			//return first entry
			return member.get(0);
		}
		//return null.
		return null;
	}

	@Override
	public boolean updateMember(Member pMember) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 
	 * @param pEmailId
	 * @param pAutoCreate 
	 * 				if true,create new member with passed emailid,
	 * @return Member
	 * @throws Exception 
	 */
	@Override
	public Member getMember(String pEmailId,boolean pAutoCreate) throws Exception {
		Member member = getMember(pEmailId);
		if(member == null && pAutoCreate){
			member = createMember(null, null, null, null, null, pEmailId);
		}
		return member;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Member getMember(final String pEmailId) {
		List<Member> member = null;
		member = getMusicsTemplate().query(GET_MEMBER_BY_EMAILID,
					new PreparedStatementSetter() {
						public void setValues(PreparedStatement preparedStatement)
								throws SQLException {
							preparedStatement.setString(1, pEmailId);
						}
					}, new MemberMapper());
		if(member != null && !member.isEmpty()){
			//return first entry
			return member.get(0);
		}
		//return null.
		return null;

	}

}
